package com.nguyenbb.ball;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.*;

/**
 * The control logic and main display panel for game.
 */
public class BallWorld extends JPanel {
	private static final int UPDATE_RATE = 30; // Frames per second (fps)
	
	private static final int NUMBER = 10;
	
	private ArrayList<Ball> ballList = new ArrayList<Ball>();
	private Ball ball1; // A single bouncing Ball's instance
	private Ball ball2; // A single bouncing Ball's instance
	private Ball ball3; // A single bouncing Ball's instance
	private Ball ball4; // A single bouncing Ball's instance
	private Ball ball5; // A single bouncing Ball's instance
	private Ball ball6; // A single bouncing Ball's instance

	private ContainerBox box; // The container rectangular box

	private DrawCanvas canvas; // Custom canvas for drawing the box/ball
	private int canvasWidth;
	private int canvasHeight;

	/**
	 * Constructor to create the UI components and init the game objects. Set
	 * the drawing canvas to fill the screen (given its width and height).
	 * 
	 * @param width
	 *            : screen width
	 * @param height
	 *            : screen height
	 */
	public BallWorld(int width, int height) {

		canvasWidth = width;
		canvasHeight = height;

		// Init the ball at a random location (inside the box) and moveAngle
		Random rand = new Random();
		int radius = 20;
		int x = rand.nextInt(canvasWidth - radius * 2 - 20) + radius + 10;
		int y = rand.nextInt(canvasHeight - radius * 2 - 20) + radius + 10;
		int speed = 5;
		int angleInDegree = rand.nextInt(360);
//		ball1 = new Ball(x, y, radius, speed, angleInDegree, Color.BLUE);
//		ball2 = new Ball(2 * x + 50, y, radius, speed, angleInDegree * (-1),
//				Color.BLUE);
//		ball3 = new Ball(x, 2 * y + 30, radius, speed, angleInDegree * (-2),
//				Color.BLUE);
//		ball4 = new Ball(x, 2 * y + 10, radius, speed - 10, angleInDegree * (1),
//				Color.BLUE);
//		ball5 = new Ball(x - 5, 2 * y, radius, speed - 5, angleInDegree * (-1),
//				Color.BLUE);
//		ball6 = new Ball(x + 10, 2 * y, radius, speed, angleInDegree * (-1),
//				Color.BLUE);
		for (int i = 0; i < NUMBER; i++) {
			ballList.add(new Ball(x*(i+1), y, radius, speed + i, angleInDegree, Color.BLUE));
		}
		
		// Init the Container Box to fill the screen
		box = new ContainerBox(0, 0, canvasWidth, canvasHeight, Color.BLACK,
				Color.WHITE);

		// Init the custom drawing panel for drawing the game
		canvas = new DrawCanvas();
		this.setLayout(new BorderLayout());
		this.add(canvas, BorderLayout.CENTER);

		// Handling window resize.
		this.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				Component c = (Component) e.getSource();
				Dimension dim = c.getSize();
				canvasWidth = dim.width;
				canvasHeight = dim.height;
				// Adjust the bounds of the container to fill the window
				box.set(0, 0, canvasWidth, canvasHeight);
			}
		});

		// Start the ball bouncing
		gameStart();
	}

	/** Start the ball bouncing. */
	public void gameStart() {
		// Run the game logic in its own thread.
		Thread gameThread = new Thread() {
			public void run() {
				while (true) {
					// Execute one time-step for the game
					gameUpdate();
					// Refresh the display
					repaint();
					// Delay and give other thread a chance
					try {
						Thread.sleep(1000 / UPDATE_RATE);
					} catch (InterruptedException ex) {
					}
				}
			}
		};
		gameThread.start(); // Invoke GaemThread.run()
	}

	/**
	 * One game time-step. Update the game objects, with proper collision
	 * detection and response.
	 */
	public void gameUpdate() {
//		ball1.moveOneStepWithCollisionDetection(box);
//		ball2.moveOneStepWithCollisionDetection(box);
//		ball3.moveOneStepWithCollisionDetection(box);
//		ball4.moveOneStepWithCollisionDetection(box);
//		ball5.moveOneStepWithCollisionDetection(box);
//		ball6.moveOneStepWithCollisionDetection(box);
		for(Ball ball: ballList) {
			ball.moveOneStepWithCollisionDetection(box);
		}
	}

	/** The custom drawing panel for the bouncing ball (inner class). */
	class DrawCanvas extends JPanel {
		/** Custom drawing codes */
		@Override
		public void paintComponent(Graphics g) {
//			super.paintComponent(g); // Paint background
//			// Draw the box and the ball
			box.draw(g);
//			ball1.draw(g);
//			ball2.draw(g);
//			ball3.draw(g);
//			ball4.draw(g);
//			ball5.draw(g);
//			ball6.draw(g);
			for(Ball ball: ballList) {
				ball.draw(g);
			}
			// Display ball's information
			// // g.setColor(Color.WHITE);
			// // g.setFont(new Font("Courier New", Font.PLAIN, 12));
			// g.drawString("Ball " + ball.toString(), 20, 30);
		}

		/** Called back to get the preferred size of the component. */
		@Override
		public Dimension getPreferredSize() {
			return (new Dimension(canvasWidth, canvasHeight));
		}
	}
}